import { User } from '../model/User';
import { StringUtil } from '../../../utils/StringUtil';
import { UserResource } from '../model/UserResource';
import { UUIDUtil } from '../../../utils/UUIDUtil';
import { RecUserTime } from '../model/RecUserTime';
import { UserClassList } from '../model/UserClassList';

export class SqlLibUser {
  constructor() {}

  static queryCurrentUser(sqlLib) {
    if (!sqlLib) {
      return 'error';
    }
    let sql = 'select * from t_user order by active_time desc limit 1';
    let result = sqlLib.runSql(sql);
    if (sqlLib.checkSqlError(result)) {
      if (result[0]) {
        let temp = new User();
        return temp.mappingModel(result[0]);
      } else {
        return null;
      }
    } else {
      return result;
    }
  }

  static queryLessonResource(sqlLib, userId, lessonId) {
    if (!sqlLib) {
      return 'error';
    }
    let sql = StringUtil.format("select * from t_user_resource where user_id = '{0}' and lesson_id = '{1}' order by sort_index", userId, lessonId);
    let result = sqlLib.runSql(sql);
    if (sqlLib.checkSqlError(result)) {
      if (result[0]) {
        let temp = new UserResource();
        let resources = [];
        result.forEach((userRes) => {
          resources.push(temp.mappingModel(userRes));
        });
        return resources;
      } else {
        return [];
      }
    } else {
      return result;
    }
  }

  
  static updateUserTime(sqlLib, userId, nowDay, nowTime) {
    let sql = StringUtil.format("select * from t_rec_user_time where user_id = '{0}' and time_day = '{1}'", userId, nowDay);
    let result = sqlLib.runSql(sql);
    if (sqlLib.checkSqlError(result)) {
      let userTime = new RecUserTime();
      if (result[0]) {
        userTime = userTime.mappingModel(result[0]);
        userTime.minuteCount = userTime.minuteCount + 1;
        userTime.gmtCreate = nowTime;
        sqlLib.updateObjectById(userTime, false, true);
      } else {
        userTime.id = UUIDUtil.uuid();
        userTime.userId = userId;
        userTime.timeDay = nowDay;
        userTime.gmtCreate = nowTime;
        userTime.minuteCount = 1;
        sqlLib.insertObject(userTime);
      }
    } else {
      return result;
    }
  }

  static queryUserClassList(sqlLib, userId) {
    let sql = StringUtil.format("select * from t_user_teaching_progress where user_id = '{0}'", userId);
    let res = sqlLib.runSql(sql);
    //报错则创建表格
    if (res === 'sql error') {
      let createSql = `CREATE TABLE t_user_teaching_progress (
        id text PRIMARY KEY NOT NULL,
        user_id text NOT NULL,
        book_id text NOT NULL,
        current_lesson text NOT NULL,
        class_name text NOT NULL,
        publish_name text NOT NULL,
        is_active text,
        reverse1 text,
        reverse2 text,
        reverse3 text)`;
      let createRes = sqlLib.runSql(createSql);
      if (createRes !== 'sql error') return [];
      else return 'not search table';
    } else {
      let temp = new UserClassList()
      if(res[0]){
        let classList = []
        res.forEach(item => {
          classList.push(temp.mappingModel(item))
        }) 
        return classList
      }else{
        return res
      }
    }
  }
  static deleteOneClassById(sqlLib, id,userId){
    let sql = StringUtil.format("delete from t_user_teaching_progress where id = '{0}' and user_id = '{1}'",id,userId)
    let res = sqlLib.runSql(sql);
    return res
  }

  static updateOneClassById(sqlLib,data,id,userId){
    const keys = Object.keys(data)
    let temp = new UserClassList()
    let sql = 'update t_user_teaching_progress set '
    keys.forEach((key,index) => {
      sql += (`${temp.dbMapper()[key]} = '${data[key]}'` + (index === keys.length -1 ? '' : ','))
    })
    sql += " where id = '{0}' and user_id = '{1}'"
    let formatSql =  StringUtil.format(sql,id,userId)
    console.log(formatSql)
    let res = sqlLib.runSql(formatSql);
    return res
  }
}
